package com.framework.core.utils;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * IP工具类
 * @author wangzhiqiang
 * @date 2018年6月28日
 *
 */
public class IPUtil {
	
	/**
	 * 获取客户端IP
	 * @param request
	 * @return
	 */
    public static String getIpAddr(HttpServletRequest request) {
    	 String ip = request.getHeader("X-Real-IP");
         if(!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip))
             return ip;
         ip = request.getHeader("X-Forwarded-For");
         if(!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip))
         {
             int index = ip.indexOf(',');
             if(index != -1)
                return ip.substring(0, index);
             else
                 return ip;
         }
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
             ip = request.getHeader("Proxy-Client-IP");
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
             ip = request.getHeader("WL-Proxy-Client-IP");
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
             ip = request.getHeader("HTTP_CLIENT_IP");
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
             ip = request.getHeader("HTTP_X_FORWARDED_FOR");
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
             ip = request.getRemoteAddr();
         return ip;
    }
}
